package com.crm.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.SysInstitution;
import com.crm.service.SysInstitutionService;
import com.crm.util.ResponseUtil;
import com.crm.util.ValidateUtil;
import com.github.pagehelper.Page;

/**  
 * ClassName:SysMenuController <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     2016年10月26日 下午2:19:38 <br/>  
 * @author   hanlinxiu  
 * @version    
 * @since    JDK 1.8  
 * @see        
 */
@Controller
@RequestMapping("/institution")
public class SysInstitutionController {
	
	@Autowired
	private SysInstitutionService sysinstitutionService;
	
	@RequestMapping("/index")
	public String index(Page<SysInstitution> page, SysInstitution t, ModelMap map){
		
		return "sysconf/institution";
	}
	
	@RequestMapping("/getAll")
	@ResponseBody
	public void getAll(HttpServletResponse response,HttpServletRequest request, Page<SysInstitution> page, SysInstitution t){
		
		Page<SysInstitution> list = (Page<SysInstitution>) sysinstitutionService.selectAll(page, t);
		
		ResponseUtil.resultPage(response,request,list);
	}
	
	@RequestMapping("/getById")
	@ResponseBody
	public Map<String, Object> getById(String id){
		
		Map<String, Object> map = new HashMap<>();
		
		SysInstitution sysInstitution = sysinstitutionService.selectByPrimaryKey(id);
		
		map.put("record", sysInstitution);
		
		map.put("status", "success");
		map.put("msg", "获取成功！");
		
		return map;
	}
	
	@RequestMapping("/addOrUpdateRecord")
	@ResponseBody
	public Map<String, Object> addOrUpdateRecord(SysInstitution record){
		
		Map<String, Object> map = new HashMap<>();
		
		if(ValidateUtil.isEmpty(record.getInstitutionName()))
			throw new RuntimeException("名称不能为空！");
		
		if(record.getSysDepartment() == null)
			throw new RuntimeException("部门ID不能为空！");
		
		if(!ValidateUtil.isEmpty(record.getInstitutionId())){
			sysinstitutionService.updateByPrimaryKey(record);
		}else{
			sysinstitutionService.insert(record);
		}
		
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/deleteRecord")
	@ResponseBody
	public Map<String, Object> deleteRecord(SysInstitution record){
		
		Map<String, Object> map = new HashMap<>();
		
		record.setDeleteMark(0);
		sysinstitutionService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "删除成功！");
		
		return map;
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public Map<String, Object> updateRecord(SysInstitution record){
		
		Map<String, Object> map = new HashMap<>();
		
		sysinstitutionService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "修改成功！");
		
		return map;
	}
	
}
  
